// pages/list/list.js
var app = getApp();
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['外卖', '点餐', '预定'],
    wm:'外卖',
    dc:'点餐',
    yd:'预定',
    activeIndex: 0,
    dndd: [],
    wmdd: [],
    ywc:[],
    hiddenmodalput: true,
        //可以通过hidden是否掩藏弹出框的属性，来指定那个弹出框 
    orderid:null,
    tmoney:null,
    remark:null,
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
  },
  chzf:function(){
    wx.showModal({
      title: '提示',
      content: '请用餐结束后到前台付款',
      showCancel: false,
    })
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  tabClick: function (e) {
    this.setData({
      activeIndex: e.currentTarget.id
    });
  },
  tmoney:function(e){
    console.log(e)
    this.setData({
      tmoney:e.detail.value
    })
  },
  remark:function(e){
    this.setData({
      remark: e.detail.value
    })
  },
  //取消按钮  
  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认  
  confirm: function () {
    var orderid = this.data.orderid;
    var remark = this.data.remark;
    var tmoney = this.data.tmoney;
var that = this;
    if(!orderid || !remark || !tmoney){
      wx.showToast({
        title: '信息不完整',
      })
      return false;
    }
    app.util.request({
      'url': 'entry/wxapp/Tsqtk',
      'cachetime': '0',
      data: { order_id:orderid,remark:remark,tmoney:tmoney },
      success: function (res) {
        console.log(res.data)
        if (res.data == '1') {
          wx.showToast({
            title: '申请成功',
            icon: 'success',
            duration: 1000,
          })
          setTimeout(function () {
            that.reLoad()
          }, 1000)
        }
        else {
          wx.showToast({
            title: '请重试',
            icon: 'loading',
            duration: 1000,
          })
        }
      },
    })
    this.setData({
      hiddenmodalput: true
    })
  },  


  tsqtk:function(e){
    var that = this;
    console.log('申请退款' + e.currentTarget.dataset.orderid)

    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput,
      orderid: e.currentTarget.dataset.orderid
    })
  },
  onPay: function (e) {
    var wmform_id = 'the formId is a mock one';
    var wmddid = e.currentTarget.dataset.wmddid
    var that = this;
    var imglink = wx.getStorageSync('imglink')
    var wmddinfo = [];
    app.util.request({
      'url': 'entry/wxapp/OrderInfo',
      'cachetime': '0',
      data: { id: wmddid },
      success: function (res) {
        var cov = that.data.covers;
        wmddinfo = res.data;
        var storeid = wmddinfo.store.id,
          user_id = wx.getStorageSync('users').id,
          openid = wx.getStorageSync('openid'),
          money = wmddinfo.order.money,
          order_id = wmddinfo.order.id,
          coupons_id = wmddinfo.order.coupons_id,
          voucher_id = wmddinfo.order.voucher_id,
          area = wmddinfo.order.area,
          lat = wmddinfo.order.lat,
          lng = wmddinfo.order.lng,
          ps_mode = wmddinfo.store.ps_mode;

        console.log(storeid, user_id, openid, money, order_id, coupons_id, voucher_id)
        if (wmform_id == '') {
          wx: wx.showToast({
            title: '网络不好',
            icon: 'loading',
          })
        } else {   
          app.util.request({
            'url': 'entry/wxapp/pay',
            'cachetime': '0',
            data: { openid: openid, order_id: order_id, money: money },
            success: function (res) {
              console.log(res)
              // 支付
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.package,
                'signType': res.data.signType,
                'paySign': res.data.paySign,
                'success': function (res) {
                  wx.showToast({
                    title: '支付成功',
                    duration: 1000
                  })
                  // 打印机
                  app.util.request({
                    'url': 'entry/wxapp/Print',
                    'cachetime': '0',
                    data: { order_id: order_id },
                    success: function (res) {
                      console.log(res)
                    },
                  })
      
                  if (ps_mode == '1') {
                    //达达
                    app.util.request({
                      'url': 'entry/wxapp/dada',
                      'cachetime': '0',
                      data: { area: area, order_id: order_id, lat: lat, lng: lng },
                      success: function (res) {
                        console.log(res)
                      },
                    })
                  }
                  if (that.data.ptxx.is_email == '1') {
                    app.util.request({
                      'url': 'entry/wxapp/email',
                      'cachetime': '0',
                      data: { store_id: storeid, type: '外卖' },
                      success: function (res) {
                        console.log(res)
                      },
                    })
                  }
                  // 改变订单状态
                  app.util.request({
                    'url': 'entry/wxapp/PayOrder',
                    'cachetime': '0',
                    data: { user_id: user_id, order_id: order_id, coupons_id: coupons_id, voucher_id: voucher_id },
                    success: function (res) {
                      console.log(res)
                      // 支付成功一秒后返回
                      setTimeout(function () {
                        wx.navigateBack({

                        })
                      }, 1000)
                      console.log(wmform_id)
                      // 下单发送模板消息
                      app.util.request({
                        'url': 'entry/wxapp/Message',
                        'cachetime': '0',
                        data: { openid: openid, form_id: wmform_id, id: order_id },
                        success: function (res) {
                          console.log(res)

                        },
                      })
                      // 支付完成发送短信给商家
                      app.util.request({
                        'url': 'entry/wxapp/sms',
                        'cachetime': '0',
                        data: { store_id: storeid },
                        success: function (res) {
                          console.log(res)

                        },
                      })
                    },
                  })
                },
                'complete': function (res) {
                  console.log(res.errMsg);
                  wx.showToast({
                    title: '取消支付',
                    icon: 'loading',
                    duration: 1000
                  })
                  // setTimeout(function () {
                  //   wx.switchTab({
                  //     url: '../list/list',
                  //   })
                  // }, 1000)
                }
              })
            },
          })
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });

    var imglink = wx.getStorageSync('imglink')
    // 网址信息
    var activeindex = options.activeindex || '';
    this.setData({
      activeIndex: activeindex,
      imglink: imglink,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
    // 跳转退款页面
  torules:function(e){
    console.log('申请退款' + e.currentTarget.dataset.orderid)
    wx.navigateTo({
      url: '../refund/refund?oid=' + e.currentTarget.dataset.orderid,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var ptxx = wx.getStorageSync('bqxx')
    console.log(ptxx)
    var that = this
    app.util.request({
      'url': 'entry/wxapp/system',
      'cachetime': '0',
      success: function (res) {
        console.log(res)
        if (res.data.wm_name!=''){
          that.setData({
            wm: res.data.wm_name,
          })
        }
        if (res.data.dc_name != '') {
          that.setData({
            dc: res.data.dc_name,
          })
        }
        if (res.data.yd_name != '') {
          that.setData({
            yd: res.data.yd_name,
          })
        }
        var day = Number(res.data.day)
        console.log(day)
        if (day == 0) {
          day = 1
        }
        that.setData({
          day: day,
        })
      },
    })
    if (ptxx.more == '1') {
      var nbcolor = wx.getStorageSync('bqxx').color
    }
    if (ptxx.more == '2') {
      var nbcolor = wx.getStorageSync('nbcolor')
    }
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: "#ffffff",
    })
    this.setData({
      color: nbcolor,
    })
    console.log('ddonShow')
    this.reLoad();
  },
  reLoad: function () {
    var uid = wx.getStorageSync('users').id;
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/MyReservation',
      'cachetime': '0',
      data: { user_id: uid },
      success: function (res) {
        console.log(res)
        that.setData({
          ydlist: res.data
        })
      },
    })
    app.util.request({
      'url': 'entry/wxapp/myorder',
      'cachetime': '0',
      data: { user_id: uid },
      success: function (res) {
        console.log(res)
        var dndd = [], wmdd = [],ywc = [];
        for (var i = 0; i < res.data.length; i++) {
          // if (res.data[i].type == '1') {
          //   wmdd.push(res.data[i])
          //   that.setData({ dndd: dndd, wmdd: wmdd })
          // }
          // else {
          //   dndd.push(res.data[i])
          //   that.setData({ dndd: dndd, wmdd: wmdd })
          // }
          if (res.data[i].state == '1' || res.data[i].dn_state == '1') {//待付款
            wmdd.push(res.data[i])
            that.setData({ dndd: dndd, wmdd: wmdd, ywc: ywc })
          }
          else if (res.data[i].state == '3' || res.data[i].state == '2' || res.data[i].state == '0' && res.data[i].type == 1) {//待配送
            dndd.push(res.data[i])
            that.setData({ dndd: dndd, wmdd: wmdd, ywc: ywc })
          }else{//已完成
            ywc.push(res.data[i]);
            that.setData({ dndd: dndd, wmdd: wmdd,ywc:ywc })
          }
        }
        // return false;
        for (let i = 0; i < wmdd.length; i++) {
          if (wmdd[i].state == '3') {
            var end = util.formatTime(new Date).substring(0, 10).replace(/\//g, "-")
            var start = wmdd[i].time.substring(0, 10)
            console.log(start, end)
            var xcts = util.DateDiff(start, end)
            console.log(xcts, that.data.day)
            if (xcts >= that.data.day) {
              app.util.request({
                'url': 'entry/wxapp/Complete',
                'cachetime': '0',
                data: { id: wmdd[i].id },
                success: function (res) {
                  console.log(res.data)
                  if (res.data == '1') {
                    console.log('自动确认收货')
                    that.reLoad()
                  }
                },
              })
            }
          }
        }
        console.log(dndd, wmdd)
      },
    })
  },
  //取消预约
  qxyy: function (e) {
    var that = this;
    console.log('取消预约' + e.currentTarget.dataset.yyid)
    wx.showModal({
      title: '提示',
      content: '确定取消预约么',
      success: function (res) {
        var that= this;
        if (res.confirm) {
          var that = this;
          console.log('用户点击确定')
          app.util.request({
            'url': 'entry/wxapp/CancelReservation',
            'cachetime': '0',
            data: { id: e.currentTarget.dataset.yyid },
            success: function (res) {
              console.log(res.data)
              if (res.data == '1') {
                wx.showToast({
                  title: '取消成功',
                  icon: 'success',
                  duration: 1000,
                })
                setTimeout(function () {
                  that.reLoad()
                }, 1000)
              }
              else {
                wx.showToast({
                  title: '请重试',
                  icon: 'loading',
                  duration: 1000,
                })
              }
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //查看详情
  ckxq: function (e) {
    var that = this;
    console.log('查看详情' + e.currentTarget.dataset.yyid)
    wx.navigateTo({
      url: 'reserveinfo/reserveinfo?yyid=' + e.currentTarget.dataset.yyid,
    })
  },
  //取消订单
  qxdd: function (e) {
    var that = this;
    console.log('取消订单' + e.currentTarget.dataset.wmddid)
    wx.showModal({
      title: '提示',
      content: '确定取消订单么',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          app.util.request({
            'url': 'entry/wxapp/CancelOrder',
            'cachetime': '0',
            data: { order_id: e.currentTarget.dataset.wmddid },
            success: function (res) {
              console.log(res.data)
              if (res.data == '1') {
                wx.showToast({
                  title: '取消成功',
                  icon: 'success',
                  duration: 1000,
                })
                setTimeout(function () {
                  that.reLoad()
                }, 1000)
              }
              else {
                wx.showToast({
                  title: '请重试',
                  icon: 'loading',
                  duration: 1000,
                })
              }
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //外卖立即支付
  ljzf: function (e) {
    var that = this;
    console.log('立即支付' + e.currentTarget.dataset.wmddid)
    wx.navigateTo({
      url: 'waim/waim?wmddid=' + e.currentTarget.dataset.wmddid,
    })
  },
  //提醒商家
  txsj: function (e) {
    var that = this;
    console.log('提醒商家' + e.currentTarget.dataset.wmddtel)
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.wmddtel,
    })
  },
  //联系商家
  lxsj: function (e) {
    var that = this;
    console.log('联系商家' + e.currentTarget.dataset.wmddtel)
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.wmddtel,
    })
  },
  //确认收货
  qrsh: function (e) {
    var that = this;
    console.log('确认收货' + e.currentTarget.dataset.wmddid)
    wx.showModal({
      title: '提示',
      content: '确定收货么',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          app.util.request({
            'url': 'entry/wxapp/Complete',
            'cachetime': '0',
            data: { id: e.currentTarget.dataset.wmddid },
            success: function (res) {
              console.log(res.data)
              if (res.data == '1') {
                wx.showToast({
                  title: '收货成功',
                  icon: 'success',
                  duration: 1000,
                })
                setTimeout(function () {
                  that.reLoad()
                }, 1000)
              }
              else {
                wx.showToast({
                  title: '请重试',
                  icon: 'loading',
                  duration: 1000,
                })
              }
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //删除订单
  scdd: function (e) {
    var that = this;
    console.log('删除订单' + e.currentTarget.dataset.wmddid)
    wx.showModal({
      title: '提示',
      content: '删除订单么',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          app.util.request({
            'url': 'entry/wxapp/DelOrder',
            'cachetime': '0',
            data: { order_id: e.currentTarget.dataset.wmddid },
            success: function (res) {
              console.log(res.data)
              if (res.data == '1') {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 1000,
                })
                setTimeout(function () {
                  that.reLoad()
                }, 1000)
              }
              else {
                wx.showToast({
                  title: '请重试',
                  icon: 'loading',
                  duration: 1000,
                })
              }
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //删除预约
  scyy: function (e) {
    var that = this;
    console.log('删除订单' + e.currentTarget.dataset.yyid)
    wx.showModal({
      title: '提示',
      content: '删除订单么',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          app.util.request({
            'url': 'entry/wxapp/DelYd',
            'cachetime': '0',
            data: { id: e.currentTarget.dataset.yyid },
            success: function (res) {
              console.log(res.data)
              if (res.data == '1') {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 1000,
                })
                setTimeout(function () {
                  that.reLoad()
                }, 1000)
              }
              else {
                wx.showToast({
                  title: '请重试',
                  icon: 'loading',
                  duration: 1000,
                })
              }
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //申请退款
  sqtk: function (e) {
    var that = this;
    console.log('申请退款' + e.currentTarget.dataset.wmddid)
    wx.showModal({
      title: '提示',
      content: '申请退款么',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          app.util.request({
            'url': 'entry/wxapp/Tuik',
            'cachetime': '0',
            data: { order_id: e.currentTarget.dataset.wmddid },
            success: function (res) {
              console.log(res.data)
              if (res.data == '1') {
                wx.showToast({
                  title: '申请成功',
                  icon: 'success',
                  duration: 1000,
                })
                setTimeout(function () {
                  that.reLoad()
                }, 1000)
              }
              else {
                wx.showToast({
                  title: '请重试',
                  icon: 'loading',
                  duration: 1000,
                })
              }
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //申请退款
  yysqtk: function (e) {
    var that = this;
    console.log('预约申请退款' + e.currentTarget.dataset.yyid)
    wx.showModal({
      title: '提示',
      content: '申请退款么',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          app.util.request({
            'url': 'entry/wxapp/YdRefund',
            'cachetime': '0',
            data: { id: e.currentTarget.dataset.yyid },
            success: function (res) {
              console.log(res.data)
              if (res.data == '1') {
                wx.showToast({
                  title: '申请成功',
                  icon: 'success',
                  duration: 1000,
                })
                setTimeout(function () {
                  that.reLoad()
                }, 1000)
              }
              else {
                wx.showToast({
                  title: '请重试',
                  icon: 'loading',
                  duration: 1000,
                })
              }
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //再来一单
  zlyd: function (e) {
    // var that = this;
    // console.log('再来一单' + e.currentTarget.dataset.wmddid)
    // wx.navigateTo({
    //   url: 'waim/waim?wmddid=' + e.currentTarget.dataset.wmddid,
    // })
    wx.switchTab({
      url: '../home/home',
    })
  },
  //评价
  pingjia: function (e) {
    var that = this;
    console.log('评价' + e.currentTarget.dataset.wmddid)
    wx.navigateTo({
      url: '../comment/comment?wmddid=' + e.currentTarget.dataset.wmddid,
    })
  },
  //店内立即支付
  dnljzf: function (e) {
    var that = this;
    console.log('立即支付' + e.currentTarget.dataset.dnddid)
    wx.navigateTo({
      url: 'choose/choose?dnddid=' + e.currentTarget.dataset.dnddid,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
  onPullDownRefresh: function () {
    this.reLoad();
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})